package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.ColumnServiceClient;
import com.hxkj.client.service.OptionColumnServiceClient;
import com.hxkj.domain.po.*;
import com.hxkj.utils.TemplateModelUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName ColumnDirective 单个分类树标签解析
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/7/20 15:02
 * @Version 1.0
 */
@Component
@Log4j
public class ColumnDirective extends AbstractDirective {

    @Autowired
    private OptionColumnServiceClient optionColumnServiceClient;

    @Autowired
    private ColumnServiceClient columnServiceClient;


    /**
     * 输入参数，分类ID
     */
    public static final String COLUMN_ID = "columnId";
    /**
     * 输入参数，站点ID
     */
    public static final String SITE_ID = "siteId";

    /**
     * 输入参数，获取条数
     */
    public static final String COUNT = "count";

    /**
     * 分类内容
     */
    public static final String COLUMN_TREE = "columnTree";



    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            //Mr.zheng:获取参数
            Long columnIdEnv = TemplateModelUtils.converLong(env.getVariable("columnId"));
            Long columnId = super.getLong(COLUMN_ID, params) == null ? columnIdEnv : super.getLong(COLUMN_ID, params);
            Long siteId = super.getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : super.getLong(SITE_ID, params);
            //Mr.zheng:默认取5条文章
            int count = super.getInt(COUNT, params) == null ? -1 : super.getInt(COUNT, params);
            String note = super.getNote(env);
            if (columnId == null) {
                log.info("分类ID为空");
            }

            Column column = new Column();
            column.setColumnId(columnId);
            column.setSiteId(siteId);
            //Mr.zheng:封装当前选中的分类信息
            column = columnServiceClient.getColumn(column);

            ContentMapping contentMapping = new ContentMapping();
            contentMapping.setColumnId(columnId);

            if (column == null) {
                column = new Column();
            }
            if (column.getParentColumnId() != null && column.getParentColumnId() != -1) {
                column = getColumnByParent(column.getParentColumnId());//获取顶级分类
            }

            column.setCount(count);
            OptionColumnTree columnTree = optionColumnServiceClient.getColumnTree(column);

//            columnTree = modifyChecked(columnTree, columnId);//修改当前分类选中状态

            env.setVariable(COLUMN_TREE, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(columnTree));


            String userToken = super.getUserToken(env);
            Writer out = env.getOut();
            if (body != null) {
                if (StringUtils.isEmpty(note)) {
                    body.render(env.getOut());
                } else {
                    Map<String, String> maps = new LinkedHashMap<>();
                    try {
                        super.print(body, maps, env, userToken, params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new RuntimeException("分类所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
